<template>
	<view class="warp">
		<image src="http://videooss.dingding10.top/material/web/l1.png" mode="widthFix"></image>
		<view class="Main-warp">
			<view class="Main-box">
				<view>
					充 <text class="costText">{{DataObj.num}}</text> 元
				</view>
				<view>
					得 <text class="sendText">{{DataObj.money}}</text> k币+送 <text
						class="sendText">{{DataObj.saleSend}}</text> K币
				</view>
				<button class="costBtn" type="default" @click="handleClick">立即充值</button>
			</view>
		</view>
		<image class="tips" src="http://videooss.dingding10.top/material/web/l2.png" mode="widthFix"></image>
	</view>
</template>

<script>
	import {
		getBookorder,
		getBookGold
	} from '@/module/index.js'
	export default {
		data() {
			return {
				DataObj: {
					num: `19元`,
					money: 1900,
					saleSend: 1100,
					bookNum: 3000
				}
			}
		},
		onLoad(e) {
			this.$set(this.DataObj, "num", e.saleNum)
			this.$set(this.DataObj, "money", e.saleNum * 100)
			this.$set(this.DataObj, "saleSend", e.saleSend)
			this.$set(this.DataObj, "bookNum", e.saleNum * 100 +
				e.saleSend * 1
			)
			console.log(e, this.DataObj)
		},
		onReady() {

		},
		// 下拉刷新
		onPullDownRefresh() {

		},
		methods: {
			handleClick() {
				this.getBookorder(this.DataObj)
			},
			getBookorder(item) {
				let obj = {
					total_fee: item.money,
					name: item.num,
					number: item.bookNum
				}
				getBookorder(obj).then((res) => {
					var self = this
					if (res.code === 200) {
						console.log(res.data)
						var record_id = res.data.pay_record_id
						uni.requestPayment({
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: 'MD5',
							paySign: res.data.paySign,
							success(res) {
								console.log(res)
								setTimeout(function() {
									self.getBookGold()
								}, 1000);
							},
							fail(err) {
								console.log(err)
								if (err.errMsg = "requestPayment:fail cancel") {
									uni.showToast({
										title: `取消支付`,
										icon: 'none'
									});
								} else {
									uni.showToast({
										title: `支付失败`,
										icon: 'none'
									});
								}
							}
						});
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getBookGold() {
				getBookGold().then((res) => {
					if (res.code === 200) {
						console.log(res)
						// this.book_currency = res.data.data.book_currency
						// this.is_members = res.data.data.is_members
						// this.expiration_time = res.data.data.expiration_time
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
				}).catch(err => {
					console.log(err)
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		position: relative;
		overflow: hidden;
		background-color: #4c1cb0;
	}


	.Main-box {
		background-color: #fff;
		text-align: center;
		line-height: 80rpx;
		padding: 20px 0rpx;
		border-radius: 12rpx;
		width: 80%;
		position: absolute;
		left: 50%;
		transform: translateX(-50%);

		.costText {
			color: orange;
			font-size: 34rpx;
		}

		.sendText {
			color: orange;
			font-size: 40rpx;
		}

		.costBtn {
			background-color: orange;
			width: 200rpx;
			height: 90rpx;
		}
	}

	.tips {
		position: absolute;
		bottom: 60rpx;
		left: 50%;
		transform: translateX(-50%);
	}
</style>
